﻿using System;

using GalaSoft.MvvmLight;

using Stock.Model;
using Stock.Util;

namespace Stock.ViewModel
{
    public class StockViewModel : ViewModelBase
    {
        public StockViewModel()
        {

        }

        public StockViewModel(StockModel model, bool eyeOpend)
        {
            No = model.No;
            MarketType = model.MarketType;
            MktNum = model.MktNum;
            Code = model.Code;
            Name = model.Name;
            Price = model.Price;
            Change = model.Change;
            ChangePercent = model.ChangePercent;
            Type = model.Type;
            Number = model.Number;
            CostPrice = model.CostPrice;
            UpdatePrice(eyeOpend);
        }

        private int _no;

        /// <summary>
        /// 排序字段 值越大 越靠前
        /// </summary>
        public int No { get => _no; set => Set(ref _no, value); }

        private string _marketType;

        public string MarketType { get => _marketType; set => Set(ref _marketType, value); }

        /// <summary>
        /// 1=上证 0=深证
        /// </summary>
        private string _mktNum;
        public string MktNum { get => _mktNum; set => Set(ref _mktNum, value); }

        private string _code;
        /// <summary>
        /// 股票代码
        /// </summary>
        public string Code { get => _code; set => Set(ref _code, value); }

        private string _name;
        /// <summary>
        /// 股票名称
        /// </summary>
        public string Name { get => _name; set => Set(ref _name, value); }

        private double _price;
        /// <summary>
        /// 股票当前价格
        /// </summary>
        public double Price { get => _price; set => Set(ref _price, value); }

        private double _change;
        /// <summary>
        /// 涨幅/跌幅
        /// </summary>
        public double Change { get => _change; set => Set(ref _change, value); }

        private string _changePercent;
        /// <summary>
        /// 涨幅/跌幅 百分比
        /// </summary>
        public string ChangePercent { get => _changePercent; set => Set(ref _changePercent, value); }

        private string _type;
        /// <summary>
        /// 1=股票 0=基金
        /// </summary>
        public string Type { get => _type; set => Set(ref _type, value); }

        private double _number;
        private string _numberString;
        /// <summary>
        /// 持有数量
        /// </summary>
        public double Number { get => _number; set => Set(ref _number, value); }

        public string NumberString { get => _numberString; set => Set(ref _numberString, value); }

        private double _costPrice;
        private string _costPriceString;
        /// <summary>
        /// 成本价格
        /// </summary>
        public double CostPrice { get => _costPrice; set => Set(ref _costPrice, value); }

        public string CostPriceString { get => _costPriceString; set => Set(ref _costPriceString, value); }

        private double _todayPrice;
        private string _todayPriceString;
        /// <summary>
        /// 今日盈亏
        /// </summary>
        public double TodayPrice { get => _todayPrice; set => Set(ref _todayPrice, value); }

        public string TodayPriceString { get => _todayPriceString; set => Set(ref _todayPriceString, value); }

        private double _totalPrice;
        private string _totalPriceString;
        /// <summary>
        /// 总盈亏
        /// </summary>
        public double TotalPrice { get => _totalPrice; set => Set(ref _totalPrice, value); }

        public string TotalPriceString { get => _totalPriceString; set => Set(ref _totalPriceString, value); }

        private string _profitPercent;

        /// <summary>
        /// 收益百分比
        /// </summary>
        public string ProfitPercent { get => _profitPercent; set => Set(ref _profitPercent, value); }

        private string _updateTime;

        /// <summary>
        /// 更新时间
        /// </summary>
        public string UpdateTime { get => _updateTime; set => Set(ref _updateTime, value); }

        public StockModel ToModel()
        {
            StockModel model = new StockModel
            {
                No = _no,
                MarketType = _marketType,
                MktNum = _mktNum,
                Code = _code,
                Name = _name,
                Type = _type,
                CostPrice = _costPrice,
                Number = _number
            };

            return model;
        }

        public void Update(StockModel model, bool eyeOpend)
        {
            No = model.No;
            MarketType = model.MarketType;
            MktNum = model.MktNum;
            Code = model.Code;
            Name = model.Name;
            Price = model.Price;
            Change = model.Change;
            ChangePercent = model.ChangePercent;
            Type = model.Type;
            Number = model.Number;
            CostPrice = model.CostPrice;
            UpdatePrice(eyeOpend);
        }

        public void UpdatePrice(bool eyeOpend)
        {
            TodayPrice = StringUtil.ToFixed(Change * Number);
            TotalPrice = StringUtil.ToFixed((Price - CostPrice) * Number);
            if (Number > 0)
            {
                ProfitPercent = $"{StringUtil.ToFixed((Price - CostPrice) * 100 / CostPrice)}%";
            }
            else
            {
                ProfitPercent = "--";
            }
            NumberString = StringUtil.ToEyeString(_number, eyeOpend);
            CostPriceString = StringUtil.ToEyeString(_costPrice, eyeOpend);
            TodayPriceString = StringUtil.ToEyeString(_todayPrice, eyeOpend);
            TotalPriceString = StringUtil.ToEyeString(_totalPrice, eyeOpend);
            ProfitPercent = StringUtil.ToEyeString(_profitPercent, eyeOpend);
            UpdateTime = DateTime.Now.ToString("HH:mm");
        }

        public void UpdateMarketType(StockModel model)
        {
            MarketType = model.MarketType;
        }

        public bool Valid()
        {
            if (string.IsNullOrEmpty(Code))
            {
                return false;
            }
            if (string.IsNullOrEmpty(MktNum))
            {
                return false;
            }
            if (string.IsNullOrEmpty(Type))
            {
                return false;
            }
            if (string.IsNullOrEmpty(Name))
            {
                return false;
            }
            return true;
        }

        public void Clear()
        {
            Code = "";
            Name = "";
            MktNum = "";
            Type = "";
            ChangePercent = "";
            UpdateTime = "";
            Price = Change = CostPrice = Number = 0;
            TodayPrice = TotalPrice = 0d;
        }

        public bool IsOTCFund()
        {
            return MktNum == "150";
        }

        public bool IsFund()
        {
            return Type == "0";
        }

        public override string ToString()
        {
            return $"[{Code}][{Name}]";
        }
    }
}
